package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.entity.OfferPriceLogArchive;
import com.yisu.auction.ewf.entity.PrizeArchive;
import com.yisu.auction.manager.core.mapper.OfferPriceLogArchiveBackupMapper;
import com.yisu.auction.manager.core.mapper.OfferPriceLogArchiveMapper;
import com.yisu.auction.manager.core.mapper.PrizeArchiveBackupMapper;
import com.yisu.auction.manager.core.mapper.PrizeArchiveMapper;
import com.yisu.auction.manager.core.service.IPrizeArchiveService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/10/19
 */
@Service("IPrizeArchiveService")
public class PrizeArchiveServiceImpl implements IPrizeArchiveService {
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;
    @Resource
    private OfferPriceLogArchiveMapper offerPriceLogArchiveMapper;
    @Resource
    private OfferPriceLogArchiveBackupMapper offerPriceLogArchiveBackupMapper;
    @Resource
    private PrizeArchiveBackupMapper prizeArchiveBackupMapper;

    @Override
    public boolean detailedArchive(List<Long> prizeIds) {
        PrizeArchive prizeArchive = prizeArchiveMapper.getArchiveFileByOne(prizeIds);
        if (prizeArchive == null) {
            return false;
        }
      /*  //检测当前竞拍数据是否存在真实用户购买
        if (offerPriceLogArchiveMapper.isExitTrueUserByPrizeId(prizeArchive.getId()) > 0) {
            return false;
        }*/
        int totalSize=offerPriceLogArchiveMapper.getTotalCountByPrizeId(prizeArchive.getId());
        if(totalSize<=0)
            return false;
        int totalPage = (totalSize / 1000) + 1;
        for (int i = 0; i < totalPage; i++) {
            List<OfferPriceLogArchive> priceLogArchives = offerPriceLogArchiveMapper.getListByPrizeId(prizeArchive.getId());
            if (CollectionUtils.isEmpty(priceLogArchives)) {
                continue;
            }
            for (OfferPriceLogArchive item : priceLogArchives) {
                offerPriceLogArchiveBackupMapper.insertSelective(item);
                offerPriceLogArchiveMapper.deleteOfferPriceLogArchiveById(item.getId());
            }
        }
        prizeArchiveBackupMapper.insertSelective(prizeArchive);
        System.out.println("数据竞拍项目最终归档成功，归档ID:" + prizeArchive.getId());
        prizeArchiveMapper.deleteByPrimaryKey(prizeArchive.getId());
        return true;
    }

    /**
     * author: 赵杰
     * Business Description：获取归档数据计量
     *
     * @return
     */
    @Override
    public int getArchiveFileCount() {
        return prizeArchiveMapper.getArchiveFileCount();
    }

    /**
     * author: 赵杰
     * Business Description：获取真实用户购买prizeId
     *
     * @return
     */
    @Override
    public List<Long> getTrueUserBuyPrizeId() {
        return prizeArchiveMapper.getTrueUserBuyPrizeId();
    }
}
